{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 产品设计：购物平台\n",
    "- 对注册的用户信息人脸检测，通过年龄、性别、颜值数据等进行分类，结合购物记录进行分析，推送适合用户的产品或者推出适合的营销活动。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 价值："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对用户允许的前提下使用人脸识别的功能，收集其年龄、性别、颜值。皮肤状况。有助于辅助使用购物记录来分析用户的购物喜好，推测相同年龄段，性别的用户可能感兴趣的商品及优惠活动，皮肤状况分析在美妆及护肤类产品类别中有助于推荐用户适用的产品，提升满意度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用开放平台：Azure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"faceId\": \"7cd5e1d6-26af-40ff-bc9b-75b2332d59a1\", \"faceRectangle\": {\"top\": 79, \"left\": 352, \"width\": 60, \"height\": 60}, \"faceAttributes\": {\"headPose\": {\"pitch\": 6.4, \"roll\": -20.0, \"yaw\": -23.1}, \"gender\": \"female\", \"age\": 22.0, \"makeup\": {\"eyeMakeup\": true, \"lipMakeup\": true}, \"hair\": {\"bald\": 0.15, \"invisible\": false, \"hairColor\": [{\"color\": \"black\", \"confidence\": 0.99}, {\"color\": \"brown\", \"confidence\": 0.91}, {\"color\": \"gray\", \"confidence\": 0.48}, {\"color\": \"other\", \"confidence\": 0.28}, {\"color\": \"blond\", \"confidence\": 0.05}, {\"color\": \"red\", \"confidence\": 0.03}]}}}, {\"faceId\": \"8dd0a65b-0513-417d-a8d8-7e89e50cca0a\", \"faceRectangle\": {\"top\": 114, \"left\": 240, \"width\": 53, \"height\": 53}, \"faceAttributes\": {\"headPose\": {\"pitch\": -4.6, \"roll\": 2.1, \"yaw\": -20.0}, \"gender\": \"female\", \"age\": 20.0, \"makeup\": {\"eyeMakeup\": true, \"lipMakeup\": true}, \"hair\": {\"bald\": 0.05, \"invisible\": false, \"hairColor\": [{\"color\": \"black\", \"confidence\": 1.0}, {\"color\": \"brown\", \"confidence\": 0.71}, {\"color\": \"other\", \"confidence\": 0.5}, {\"color\": \"gray\", \"confidence\": 0.37}, {\"color\": \"blond\", \"confidence\": 0.04}, {\"color\": \"red\", \"confidence\": 0.02}]}}}]\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from pandas.io.json import json_normalize\n",
    "import pandas as pd\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = '12dce9b8d7dd4f06a49d51836d******'\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'\n",
    "\n",
    "image_url = 'http://www.nfu.edu.cn/Public/Uploads/images/2019-06-24/5d102e7c0b601.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    'returnFaceAttributes': 'age,gender,headPose,hair,makeup',\n",
    "}\n",
    "\n",
    "r = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "print(json.dumps(r.json()))\n",
    "results = r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '7cd5e1d6-26af-40ff-bc9b-75b2332d59a1',\n",
       "  'faceRectangle': {'top': 79, 'left': 352, 'width': 60, 'height': 60},\n",
       "  'faceAttributes': {'headPose': {'pitch': 6.4, 'roll': -20.0, 'yaw': -23.1},\n",
       "   'gender': 'female',\n",
       "   'age': 22.0,\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'hair': {'bald': 0.15,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 0.99},\n",
       "     {'color': 'brown', 'confidence': 0.91},\n",
       "     {'color': 'gray', 'confidence': 0.48},\n",
       "     {'color': 'other', 'confidence': 0.28},\n",
       "     {'color': 'blond', 'confidence': 0.05},\n",
       "     {'color': 'red', 'confidence': 0.03}]}}},\n",
       " {'faceId': '8dd0a65b-0513-417d-a8d8-7e89e50cca0a',\n",
       "  'faceRectangle': {'top': 114, 'left': 240, 'width': 53, 'height': 53},\n",
       "  'faceAttributes': {'headPose': {'pitch': -4.6, 'roll': 2.1, 'yaw': -20.0},\n",
       "   'gender': 'female',\n",
       "   'age': 20.0,\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'hair': {'bald': 0.05,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'brown', 'confidence': 0.71},\n",
       "     {'color': 'other', 'confidence': 0.5},\n",
       "     {'color': 'gray', 'confidence': 0.37},\n",
       "     {'color': 'blond', 'confidence': 0.04},\n",
       "     {'color': 'red', 'confidence': 0.02}]}}}]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.io.json.json_normalize(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.hair.bald</th>\n",
       "      <th>faceAttributes.hair.hairColor</th>\n",
       "      <th>faceAttributes.hair.invisible</th>\n",
       "      <th>faceAttributes.headPose.pitch</th>\n",
       "      <th>faceAttributes.headPose.roll</th>\n",
       "      <th>faceAttributes.headPose.yaw</th>\n",
       "      <th>faceAttributes.makeup.eyeMakeup</th>\n",
       "      <th>faceAttributes.makeup.lipMakeup</th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22.0</td>\n",
       "      <td>female</td>\n",
       "      <td>0.15</td>\n",
       "      <td>[{'color': 'black', 'confidence': 0.99}, {'col...</td>\n",
       "      <td>False</td>\n",
       "      <td>6.4</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>-23.1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>7cd5e1d6-26af-40ff-bc9b-75b2332d59a1</td>\n",
       "      <td>60</td>\n",
       "      <td>352</td>\n",
       "      <td>79</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.0</td>\n",
       "      <td>female</td>\n",
       "      <td>0.05</td>\n",
       "      <td>[{'color': 'black', 'confidence': 1.0}, {'colo...</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.6</td>\n",
       "      <td>2.1</td>\n",
       "      <td>-20.0</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>8dd0a65b-0513-417d-a8d8-7e89e50cca0a</td>\n",
       "      <td>53</td>\n",
       "      <td>240</td>\n",
       "      <td>114</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   faceAttributes.age faceAttributes.gender  faceAttributes.hair.bald  \\\n",
       "0                22.0                female                      0.15   \n",
       "1                20.0                female                      0.05   \n",
       "\n",
       "                       faceAttributes.hair.hairColor  \\\n",
       "0  [{'color': 'black', 'confidence': 0.99}, {'col...   \n",
       "1  [{'color': 'black', 'confidence': 1.0}, {'colo...   \n",
       "\n",
       "   faceAttributes.hair.invisible  faceAttributes.headPose.pitch  \\\n",
       "0                          False                            6.4   \n",
       "1                          False                           -4.6   \n",
       "\n",
       "   faceAttributes.headPose.roll  faceAttributes.headPose.yaw  \\\n",
       "0                         -20.0                        -23.1   \n",
       "1                           2.1                        -20.0   \n",
       "\n",
       "   faceAttributes.makeup.eyeMakeup  faceAttributes.makeup.lipMakeup  \\\n",
       "0                             True                             True   \n",
       "1                             True                             True   \n",
       "\n",
       "                                 faceId  faceRectangle.height  \\\n",
       "0  7cd5e1d6-26af-40ff-bc9b-75b2332d59a1                    60   \n",
       "1  8dd0a65b-0513-417d-a8d8-7e89e50cca0a                    53   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.top  faceRectangle.width  \n",
       "0                 352                 79                   60  \n",
       "1                 240                114                   53  "
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_attributes = df[['faceId','faceAttributes.gender','faceAttributes.age','faceAttributes.makeup.eyeMakeup','faceAttributes.makeup.lipMakeup']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.makeup.eyeMakeup</th>\n",
       "      <th>faceAttributes.makeup.lipMakeup</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7cd5e1d6-26af-40ff-bc9b-75b2332d59a1</td>\n",
       "      <td>female</td>\n",
       "      <td>22.0</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8dd0a65b-0513-417d-a8d8-7e89e50cca0a</td>\n",
       "      <td>female</td>\n",
       "      <td>20.0</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId faceAttributes.gender  \\\n",
       "0  7cd5e1d6-26af-40ff-bc9b-75b2332d59a1                female   \n",
       "1  8dd0a65b-0513-417d-a8d8-7e89e50cca0a                female   \n",
       "\n",
       "   faceAttributes.age  faceAttributes.makeup.eyeMakeup  \\\n",
       "0                22.0                             True   \n",
       "1                20.0                             True   \n",
       "\n",
       "   faceAttributes.makeup.lipMakeup  \n",
       "0                             True  \n",
       "1                             True  "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_attributes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出为表格\n",
    "#df_attributes.to_excel(\"df_attributes.xlsx\", sheet_name=\"Skinanalyze\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用开放平台：FACE++"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 年龄、性别分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1585807527,4b9e6439-b3d9-4896-8213-db5572670de1', 'time_used': 221, 'faces': [{'face_token': 'aeff33ebab46ab8aa1069f615b5257ad', 'face_rectangle': {'top': 93, 'left': 236, 'width': 117, 'height': 117}, 'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 24}}}], 'image_id': 'dUu22Tmz6Ew7VARKITu+Lg==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from pandas.io.json import json_normalize\n",
    "import pandas as pd\n",
    "\n",
    "api_secret = \"tc4HBE-hO9QlUylnjaWftwRknju******\"\n",
    "\n",
    "api_key = 'Y5ytU1HfhFFFakxlaXnG_f-qnm******'\n",
    "\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "image_url = ['https://wx2.sinaimg.cn/mw600/006837jtly1gda4ucvonjj31pc0yitk8.jpg',]\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":image_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age', \n",
    "}\n",
    "\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "print(r.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'face_token': 'aeff33ebab46ab8aa1069f615b5257ad',\n",
       "  'face_rectangle': {'top': 93, 'left': 236, 'width': 117, 'height': 117},\n",
       "  'attributes': {'gender': {'value': 'Male'}, 'age': {'value': 24}}}]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results['faces']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>aeff33ebab46ab8aa1069f615b5257ad</td>\n",
       "      <td>24</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  attributes.age.value  \\\n",
       "0  aeff33ebab46ab8aa1069f615b5257ad                    24   \n",
       "\n",
       "  attributes.gender.value  \n",
       "0                    Male  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.io.json.json_normalize(results['faces'])\n",
    "df_attributes = df[['face_token','attributes.age.value','attributes.gender.value']]\n",
    "df_attributes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 皮肤分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'time_used': 453, 'face_rectangle': {'width': 115, 'top': 94, 'height': 115, 'left': 235}, 'warning': [], 'result': {'pores_left_cheek': {'confidence': 0.99999666, 'value': 0}, 'nasolabial_fold': {'confidence': 0.17162463, 'value': 0}, 'eye_pouch': {'confidence': 0.6019428, 'value': 1}, 'forehead_wrinkle': {'confidence': 0.0010910045, 'value': 0}, 'skin_spot': {'confidence': 0.0016937742, 'value': 0}, 'acne': {'confidence': 8.55897e-05, 'value': 0}, 'pores_forehead': {'confidence': 0.9999888, 'value': 0}, 'pores_jaw': {'confidence': 0.9999888, 'value': 0}, 'left_eyelids': {'confidence': 0.9767339, 'value': 0}, 'eye_finelines': {'confidence': 0.07407945, 'value': 0}, 'dark_circle': {'confidence': 0.96334434, 'value': 1}, 'crows_feet': {'confidence': 0.00016420911, 'value': 0}, 'pores_right_cheek': {'confidence': 0.9999641, 'value': 0}, 'blackhead': {'confidence': 2.5090565e-06, 'value': 0}, 'glabella_wrinkle': {'confidence': 0.0016228743, 'value': 0}, 'mole': {'confidence': 0.16719945, 'value': 0}, 'skin_type': {'details': {'0': {'confidence': 0.033539727, 'value': 0}, '1': {'confidence': 4.9017064e-05, 'value': 0}, '2': {'confidence': 0.92202073, 'value': 1}, '3': {'confidence': 0.0443905, 'value': 0}}, 'skin_type': 2}, 'right_eyelids': {'confidence': 0.9925599, 'value': 0}}, 'request_id': '1585811750,3e46719f-84ed-4f38-8371-0d96ae31f44a'}\n"
     ]
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "from pandas.io.json import json_normalize\n",
    "import pandas as pd\n",
    "\n",
    "# 2、输入我们API_Key、密钥\n",
    "api_secret = \"tc4HBE-hO9QlUylnjaWftwRknj******\"\n",
    "\n",
    "api_key = 'Y5ytU1HfhFFFakxlaXnG_f-qnm******'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v1/skinanalyze' \n",
    "image_url = 'https://wx2.sinaimg.cn/mw600/006837jtly1gda4ucvonjj31pc0yitk8.jpg'\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":image_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret\n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "print(r.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'pores_left_cheek': {'confidence': 0.99999666, 'value': 0},\n",
       " 'nasolabial_fold': {'confidence': 0.17162463, 'value': 0},\n",
       " 'eye_pouch': {'confidence': 0.6019428, 'value': 1},\n",
       " 'forehead_wrinkle': {'confidence': 0.0010910045, 'value': 0},\n",
       " 'skin_spot': {'confidence': 0.0016937742, 'value': 0},\n",
       " 'acne': {'confidence': 8.55897e-05, 'value': 0},\n",
       " 'pores_forehead': {'confidence': 0.9999888, 'value': 0},\n",
       " 'pores_jaw': {'confidence': 0.9999888, 'value': 0},\n",
       " 'left_eyelids': {'confidence': 0.9767339, 'value': 0},\n",
       " 'eye_finelines': {'confidence': 0.07407945, 'value': 0},\n",
       " 'dark_circle': {'confidence': 0.96334434, 'value': 1},\n",
       " 'crows_feet': {'confidence': 0.00016420911, 'value': 0},\n",
       " 'pores_right_cheek': {'confidence': 0.9999641, 'value': 0},\n",
       " 'blackhead': {'confidence': 2.5090565e-06, 'value': 0},\n",
       " 'glabella_wrinkle': {'confidence': 0.0016228743, 'value': 0},\n",
       " 'mole': {'confidence': 0.16719945, 'value': 0},\n",
       " 'skin_type': {'details': {'0': {'confidence': 0.033539727, 'value': 0},\n",
       "   '1': {'confidence': 4.9017064e-05, 'value': 0},\n",
       "   '2': {'confidence': 0.92202073, 'value': 1},\n",
       "   '3': {'confidence': 0.0443905, 'value': 0}},\n",
       "  'skin_type': 2},\n",
       " 'right_eyelids': {'confidence': 0.9925599, 'value': 0}}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results['result']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "Skinanalyze = pd.io.json.json_normalize(results)\n",
    "#df.to_excel(\"df.xlsx\", sheet_name=\"人脸识别结果\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>request_id</th>\n",
       "      <th>result.acne.confidence</th>\n",
       "      <th>result.acne.value</th>\n",
       "      <th>result.blackhead.confidence</th>\n",
       "      <th>result.blackhead.value</th>\n",
       "      <th>result.crows_feet.confidence</th>\n",
       "      <th>...</th>\n",
       "      <th>result.skin_type.details.0.value</th>\n",
       "      <th>result.skin_type.details.1.confidence</th>\n",
       "      <th>result.skin_type.details.1.value</th>\n",
       "      <th>result.skin_type.details.2.confidence</th>\n",
       "      <th>result.skin_type.details.2.value</th>\n",
       "      <th>result.skin_type.details.3.confidence</th>\n",
       "      <th>result.skin_type.details.3.value</th>\n",
       "      <th>result.skin_type.skin_type</th>\n",
       "      <th>time_used</th>\n",
       "      <th>warning</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>115</td>\n",
       "      <td>235</td>\n",
       "      <td>94</td>\n",
       "      <td>115</td>\n",
       "      <td>1585811750,3e46719f-84ed-4f38-8371-0d96ae31f44a</td>\n",
       "      <td>0.000086</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000003</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000164</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0</td>\n",
       "      <td>0.922021</td>\n",
       "      <td>1</td>\n",
       "      <td>0.04439</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>453</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   face_rectangle.height  face_rectangle.left  face_rectangle.top  \\\n",
       "0                    115                  235                  94   \n",
       "\n",
       "   face_rectangle.width                                       request_id  \\\n",
       "0                   115  1585811750,3e46719f-84ed-4f38-8371-0d96ae31f44a   \n",
       "\n",
       "   result.acne.confidence  result.acne.value  result.blackhead.confidence  \\\n",
       "0                0.000086                  0                     0.000003   \n",
       "\n",
       "   result.blackhead.value  result.crows_feet.confidence  ...  \\\n",
       "0                       0                      0.000164  ...   \n",
       "\n",
       "   result.skin_type.details.0.value  result.skin_type.details.1.confidence  \\\n",
       "0                                 0                               0.000049   \n",
       "\n",
       "   result.skin_type.details.1.value  result.skin_type.details.2.confidence  \\\n",
       "0                                 0                               0.922021   \n",
       "\n",
       "   result.skin_type.details.2.value  result.skin_type.details.3.confidence  \\\n",
       "0                                 1                                0.04439   \n",
       "\n",
       "   result.skin_type.details.3.value  result.skin_type.skin_type  time_used  \\\n",
       "0                                 0                           2        453   \n",
       "\n",
       "   warning  \n",
       "0       []  \n",
       "\n",
       "[1 rows x 50 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Skinanalyze\n",
    "Skinanalyze.update(df_attributes)\n",
    "Skinanalyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出为表格\n",
    "Skinanalyze.to_excel(\"Skinanalyze.xlsx\", sheet_name=\"Skinanalyze\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用开放平台：百度智能云"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.260beb0b3b0afacff4b91a14d290f17f.315360000.1901169763.282335-19208374', 'expires_in': 2592000, 'session_key': '9mzdX+6wARO/nejRizuKrQd7HEvqTaTA/yjMimo/3MPs7WFRhl4zf5FtXDSpDBk7pX4nl43dkqwNI0mJfp7Tw1EjWILrBQ==', 'access_token': '24.d4a010ceff8a10b9f503e32d4be204b3.2592000.1588401763.282335-19208374', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi qatest_scope1 fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': '55d846ee313b4b1465a7f49ed605fb82'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "from pandas.io.json import json_normalize\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=******olBl5Q8s4ESEgUEkuH&client_secret=******oZe0Ua7jVRyfcbzEGbyqQuRAHv'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1016565254545, 'timestamp': 1585809764, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': '6dea8b4af8fd4f3e19b09cef32f9cad7', 'location': {'left': 353.12, 'top': 95.75, 'width': 61, 'height': 58, 'rotation': -20}, 'face_probability': 1, 'angle': {'yaw': 21.55, 'pitch': 1.32, 'roll': -19.71}, 'face_shape': {'type': 'round', 'probability': 0.67}, 'face_type': {'type': 'human', 'probability': 1}, 'age': 24, 'beauty': 53.26, 'expression': {'type': 'smile', 'probability': 1}, 'gender': {'type': 'female', 'probability': 1}, 'glasses': {'type': 'none', 'probability': 1}, 'race': {'type': 'yellow', 'probability': 1}, 'quality': {'occlusion': {'left_eye': 0.1, 'right_eye': 0, 'nose': 0, 'mouth': 0, 'left_cheek': 0, 'right_cheek': 0.01, 'chin_contour': 0}, 'blur': 0, 'illumination': 172, 'completeness': 1}, 'eye_status': {'left_eye': 1, 'right_eye': 1}, 'emotion': {'type': 'happy', 'probability': 1}, 'mask': {'type': 0, 'probability': 1}}]}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "人脸检测与属性分析\n",
    "'''\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"http://www.nfu.edu.cn/Public/Uploads/images/2019-06-24/5d102e7c0b601.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype,age,beauty,expression,face_shape,gender,glasses,race,\\\n",
    "quality,eye_status,emotion,face_type,mask\\\"}\"\n",
    "access_token = '[24.f0676301721c4f75152ddc1aa807dff3.2592000.1588386610.282335-19******]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'face_token': '6dea8b4af8fd4f3e19b09cef32f9cad7',\n",
       "  'location': {'left': 353.12,\n",
       "   'top': 95.75,\n",
       "   'width': 61,\n",
       "   'height': 58,\n",
       "   'rotation': -20},\n",
       "  'face_probability': 1,\n",
       "  'angle': {'yaw': 21.55, 'pitch': 1.32, 'roll': -19.71},\n",
       "  'face_shape': {'type': 'round', 'probability': 0.67},\n",
       "  'face_type': {'type': 'human', 'probability': 1},\n",
       "  'age': 24,\n",
       "  'beauty': 53.26,\n",
       "  'expression': {'type': 'smile', 'probability': 1},\n",
       "  'gender': {'type': 'female', 'probability': 1},\n",
       "  'glasses': {'type': 'none', 'probability': 1},\n",
       "  'race': {'type': 'yellow', 'probability': 1},\n",
       "  'quality': {'occlusion': {'left_eye': 0.1,\n",
       "    'right_eye': 0,\n",
       "    'nose': 0,\n",
       "    'mouth': 0,\n",
       "    'left_cheek': 0,\n",
       "    'right_cheek': 0.01,\n",
       "    'chin_contour': 0},\n",
       "   'blur': 0,\n",
       "   'illumination': 172,\n",
       "   'completeness': 1},\n",
       "  'eye_status': {'left_eye': 1, 'right_eye': 1},\n",
       "  'emotion': {'type': 'happy', 'probability': 1},\n",
       "  'mask': {'type': 0, 'probability': 1}}]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = response.json()\n",
    "r=results['result']\n",
    "s=r['face_list']\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 提取年龄、性别、颜值、情绪值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender.type</th>\n",
       "      <th>beauty</th>\n",
       "      <th>glasses.type</th>\n",
       "      <th>emotion.probability</th>\n",
       "      <th>emotion.type</th>\n",
       "      <th>expression.probability</th>\n",
       "      <th>expression.type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>24</td>\n",
       "      <td>female</td>\n",
       "      <td>53.26</td>\n",
       "      <td>none</td>\n",
       "      <td>1</td>\n",
       "      <td>happy</td>\n",
       "      <td>1</td>\n",
       "      <td>smile</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age gender.type  beauty glasses.type  emotion.probability emotion.type  \\\n",
       "0   24      female   53.26         none                    1        happy   \n",
       "\n",
       "   expression.probability expression.type  \n",
       "0                       1           smile  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.io.json.json_normalize(r['face_list'])\n",
    "need_attributes=df[['age','gender.type','beauty','glasses.type','emotion.probability','emotion.type','expression.probability','expression.type']]\n",
    "need_attributes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出为表格\n",
    "#need_attributes.to_excel(\"need_attributes.xlsx\", sheet_name=\"人脸识别结果\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
